package com.shangxian.giveithome.controller;


import com.shangxian.giveithome.annoaction.CheckLoginStatus;
import com.shangxian.giveithome.code.CheckCode;
import com.shangxian.giveithome.entity.community.Issue;
import com.shangxian.giveithome.result.Result;
import com.shangxian.giveithome.service.ApiService;
import com.shangxian.giveithome.service.IssueService;
import com.shangxian.giveithome.utils.SenInfoCheckUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/issue")
public class IssueController {
    @Autowired
    private ApiService apiService;
    @Autowired
    private IssueService issueService;

    @GetMapping("/findUserIssueList")
    @ApiOperation(value = "查询用户的文章列表")
    public Result findUserIssueList(@PageableDefault(page = 0, size = 10) Pageable pageable , @RequestParam(value = "id",required = false) Long id ,HttpServletRequest request){
        return Result.success(issueService.findUserIssueList(pageable,id,request));
    }

    @GetMapping("/findIssueDetails")
    @ApiOperation(value = "查询文章的详细信息")
    public Result findIssueDetails(@RequestParam(value = "id") Long id,  HttpServletRequest request){
        return Result.success(issueService.findIssueDetails(id,request));
    }

    @GetMapping("/findAllIssueList")
    @ApiOperation(value = "查询最新动态发布列表")
    public Result findAllIssueList(@PageableDefault(page = 0,size = 10)Pageable pageable){
        return Result.success(issueService.findAllIssue(pageable));
    }

    @GetMapping("/findAllQandAList")
    @ApiOperation(value = "查询所有问答动态列表")
    public Result findAllQandAList(@PageableDefault(page = 0,size = 10)Pageable pageable,@RequestParam(value = "screenSearch",required = false) String screenSearch){
        return Result.success(issueService.findAllQandAList(pageable,screenSearch));
    }

    @PostMapping("/IssueArticle")
    @ApiOperation(value = "发布动态")
    public Result IssueArticle(@RequestBody Issue issue, HttpServletRequest request){
        String access_token = apiService.getAccessToken();
        Boolean aBoolean = SenInfoCheckUtil.cotentFilter(access_token, issue.getContent());
        if (!aBoolean){
            return Result.error(CheckCode.Content_NOT_Illegal);
        }
        issueService.issueArticle(issue,request);
        return Result.success();
    }

    @GetMapping("/findAllAttentionUserIssueList")
    @ApiOperation(value = "查询关注用户的文章动态列表")
    @CheckLoginStatus
    public Result findAllAttentionUserIssueList(@PageableDefault(page = 0,size = 10)Pageable pageable, HttpServletRequest request){
        return Result.success(issueService.findAllAttentionUserIssueList(pageable,request));
    }

    @DeleteMapping("/deleteArticle")
    @ApiOperation(value ="删除文章" )
    @CheckLoginStatus
    public Result deleteArticle(@RequestParam("id") Long id){
        issueService.deleteArticle(id);
        return Result.delete_success();
    }



}
